/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAOs;

import DAOs.exceptions.IllegalOrphanException;
import DAOs.exceptions.NonexistentEntityException;
import DAOs.exceptions.PreexistingEntityException;
import VO.Tipopago;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import VO.Pagos;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Julian
 */
public class TipopagoJpaController {

    public TipopagoJpaController() {
        
    }
    

    public void create(Tipopago tipopago,EntityManager em1) throws PreexistingEntityException, Exception {
        if (tipopago.getPagosCollection() == null) {
            tipopago.setPagosCollection(new ArrayList<Pagos>());
        }
        EntityManager em = null;
        try {
            em = em1;
            //em.getTransaction().begin();
            Collection<Pagos> attachedPagosCollection = new ArrayList<Pagos>();
            for (Pagos pagosCollectionPagosToAttach : tipopago.getPagosCollection()) {
                pagosCollectionPagosToAttach = em.getReference(pagosCollectionPagosToAttach.getClass(), pagosCollectionPagosToAttach.getIdPago());
                attachedPagosCollection.add(pagosCollectionPagosToAttach);
            }
            tipopago.setPagosCollection(attachedPagosCollection);
            em.persist(tipopago);
            for (Pagos pagosCollectionPagos : tipopago.getPagosCollection()) {
                Tipopago oldIdTipoPagoOfPagosCollectionPagos = pagosCollectionPagos.getIdTipoPago();
                pagosCollectionPagos.setIdTipoPago(tipopago);
                pagosCollectionPagos = em.merge(pagosCollectionPagos);
                if (oldIdTipoPagoOfPagosCollectionPagos != null) {
                    oldIdTipoPagoOfPagosCollectionPagos.getPagosCollection().remove(pagosCollectionPagos);
                    oldIdTipoPagoOfPagosCollectionPagos = em.merge(oldIdTipoPagoOfPagosCollectionPagos);
                }
            }
            //em.getTransaction().commit();
        } catch (Exception ex) {
            if (findTipopago(tipopago.getIdtipoPago(),em) != null) {
                throw new PreexistingEntityException("Tipopago " + tipopago + " already exists.", ex);
            }
            throw ex;
        } 
    }

    public void edit(Tipopago tipopago,EntityManager em1) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = em1;
            //em.getTransaction().begin();
            Tipopago persistentTipopago = em.find(Tipopago.class, tipopago.getIdtipoPago());
            Collection<Pagos> pagosCollectionOld = persistentTipopago.getPagosCollection();
            Collection<Pagos> pagosCollectionNew = tipopago.getPagosCollection();
            List<String> illegalOrphanMessages = null;
            for (Pagos pagosCollectionOldPagos : pagosCollectionOld) {
                if (!pagosCollectionNew.contains(pagosCollectionOldPagos)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Pagos " + pagosCollectionOldPagos + " since its idTipoPago field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Pagos> attachedPagosCollectionNew = new ArrayList<Pagos>();
            for (Pagos pagosCollectionNewPagosToAttach : pagosCollectionNew) {
                pagosCollectionNewPagosToAttach = em.getReference(pagosCollectionNewPagosToAttach.getClass(), pagosCollectionNewPagosToAttach.getIdPago());
                attachedPagosCollectionNew.add(pagosCollectionNewPagosToAttach);
            }
            pagosCollectionNew = attachedPagosCollectionNew;
            tipopago.setPagosCollection(pagosCollectionNew);
            tipopago = em.merge(tipopago);
            for (Pagos pagosCollectionNewPagos : pagosCollectionNew) {
                if (!pagosCollectionOld.contains(pagosCollectionNewPagos)) {
                    Tipopago oldIdTipoPagoOfPagosCollectionNewPagos = pagosCollectionNewPagos.getIdTipoPago();
                    pagosCollectionNewPagos.setIdTipoPago(tipopago);
                    pagosCollectionNewPagos = em.merge(pagosCollectionNewPagos);
                    if (oldIdTipoPagoOfPagosCollectionNewPagos != null && !oldIdTipoPagoOfPagosCollectionNewPagos.equals(tipopago)) {
                        oldIdTipoPagoOfPagosCollectionNewPagos.getPagosCollection().remove(pagosCollectionNewPagos);
                        oldIdTipoPagoOfPagosCollectionNewPagos = em.merge(oldIdTipoPagoOfPagosCollectionNewPagos);
                    }
                }
            }
            //em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tipopago.getIdtipoPago();
                if (findTipopago(id,em) == null) {
                    throw new NonexistentEntityException("The tipopago with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } 
    }

    public void destroy(Integer id,EntityManager em1) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = em1;
            //em.getTransaction().begin();
            Tipopago tipopago;
            try {
                tipopago = em.getReference(Tipopago.class, id);
                tipopago.getIdtipoPago();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tipopago with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Pagos> pagosCollectionOrphanCheck = tipopago.getPagosCollection();
            for (Pagos pagosCollectionOrphanCheckPagos : pagosCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Tipopago (" + tipopago + ") cannot be destroyed since the Pagos " + pagosCollectionOrphanCheckPagos + " in its pagosCollection field has a non-nullable idTipoPago field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(tipopago);
            //em.getTransaction().commit();
        } catch(Exception ex){
            throw ex;
        }
    }

    public List<Tipopago> findTipopagoEntities(EntityManager em1) throws Exception {
        return findTipopagoEntities(true, -1, -1,em1);
    }

    public List<Tipopago> findTipopagoEntities(int maxResults, int firstResult,EntityManager em1) throws Exception {
        return findTipopagoEntities(false, maxResults, firstResult,em1);
    }

    private List<Tipopago> findTipopagoEntities(boolean all, int maxResults, int firstResult,EntityManager em1) throws Exception {
        EntityManager em = em1;
        try {
            Query q = em.createQuery("select object(o) from Tipopago as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } catch(Exception ex){
            throw ex;
        }
    }

    public Tipopago findTipopago(Integer id,EntityManager em1) throws Exception {
        EntityManager em = em1;
        try {
            return em.find(Tipopago.class, id);
        } catch(Exception ex){
            throw ex;
        }
    }

    public int getTipopagoCount(EntityManager em1) throws Exception {
        EntityManager em = em1;
        try {
            Query q = em.createQuery("select count(o) from Tipopago as o");
            return ((Long) q.getSingleResult()).intValue();
        } catch(Exception ex){
            throw ex;
        }
    }

}
